/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.moedik.controller;

import com.moedik.classes.Payment;
import com.moedik.classes.User;
import com.moedik.model.PaymentModel;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author SAMMY
 */
public class PaymentController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        if (request.getParameter("action").equals("manage")) {
            if (((User) request.getSession().getAttribute("user")).getRoleId() > 3) {
                response.sendError(403);
            } else {
                PaymentModel paymentModel = new PaymentModel();
                ArrayList<Payment> payments = paymentModel.getAll();

                request.setAttribute("payments", payments);
                request.setAttribute("title", "Manage Payments");
                request.getRequestDispatcher("payments/manage.jsp").forward(request, response);
            }
        } else if (request.getParameter("action").equals("delete")) {
            if (((User) request.getSession().getAttribute("user")).getRoleId() > 3) {
                response.sendError(403);
            } else {
                int id = Integer.parseInt(request.getParameter("id"));

                PaymentModel paymentModel = new PaymentModel();

                if (paymentModel.delete(id) == 0) {
                    response.setStatus(404);
                }
            }
        } else if (request.getParameter("action").equals("approve")) {
            if (((User) request.getSession().getAttribute("user")).getRoleId() > 3) {
                response.sendError(403);
            } else {
                int id = Integer.parseInt(request.getParameter("id"));

                PaymentModel paymentModel = new PaymentModel();

                if (paymentModel.approve(id) == 0) {
                    response.setStatus(404);
                }
            }
        } else if (request.getParameter("action").equals("confirm")) {
            boolean authenticated = (Boolean) request.getSession().getAttribute("authenticated");

            if (!authenticated) {
                response.sendError(403);
            } else {
                if (request.getMethod().equals("POST")) {
                    String billno = request.getParameter("billno").toString();
                    
                    PaymentModel paymentModel = new PaymentModel();
                    String result = paymentModel.confirm(billno);
                    
                    request.setAttribute("result", result);
                    request.setAttribute("title", "Confirm Payment");
                    request.getRequestDispatcher("payments/result.jsp").forward(request, response);
                } else {
                    request.setAttribute("title", "Confirm Payment");
                    request.getRequestDispatcher("payments/confirm.jsp").forward(request, response);
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
